<?php

namespace JsonLowCode\Support;

use JsonException;
use JsonLowCode\Helps\Js as HelpsJs;

class Js
{
    /**
     * @param mixed $data
     * @param int $flags
     * @param int $depth
     * @return HelpsJs
     * @throws JsonException
     */
    public static function from(mixed $data, int $flags = 0, int $depth = 512): HelpsJs
    {
        return HelpsJs::from($data,$flags,$depth);
    }

    /**
     * window对象
     * @param string $key
     * @return string
     */
    public static function window(string $key = ""): string
    {
        return $key?"window.{$key}":"window";
    }

    /**
     * useScope函数调用
     * @param string $key
     * @return string
     */
    public static function useScope(string $key = ""): string
    {
        return $key?"window.useScope().".$key:"window.useScope()";
    }

    /**
     * 函数中导出的变量
     * @param string $key
     * @return string
     */
    public static function exports(string $key = ""): string
    {
        return $key?"__exports__.{$key}":"__exports__";
    }

    /**
     * 当前对象
     * @param string $key
     * @return string
     */
    public static function this(string $key = ""): string
    {
        return $key?"this.{$key}":"this";
    }


    /**
     * 当前对象
     * @param string $key
     * @return string
     */
    public static function state(string $key = ""): string
    {
        return $key?"this.state.{$key}":"this.state";
    }

    /**
     * 当前对象 $refs 对象
     * @param string $key
     * @return string
     */
    public static function refs(string $key = ""): string
    {
        if ($key){
            return self::this("\$refs.{$key}");
        }else{
            return self::this("\$refs");
        }
    }

    /**
     * $el 元素节点
     * @return string
     */
    public static function el(): string
    {
        return self::this("\$el");
    }

    /**
     * __scope对象
     * @param string|null $key
     * @return string
     */
    public static function scope(string $key = null): string
    {
        if ($key){
            return self::this("__scope.{$key}");
        }else{
            return self::this("__scope");
        }
    }

    /**
     * $event
     * @return string
     */
    public static function event(): string
    {
        return self::scope("\$event");
    }

    /**
     * $hooks
     * @return string
     */
    public static function hooks(): string
    {
        return self::scope("\$hooks");
    }

    /**
     * $tools
     * @return string
     */
    public static function tools(): string
    {
        return self::scope("\$tools");
    }

    /**
     * $utils
     * @return string
     */
    public static function utils(): string
    {
        return self::scope("\$utils");
    }

    /**
     * store
     * @return string
     */
    public static function store(): string
    {
        return self::scope("\$store");
    }

    /**
     * dayjs
     * @return string
     */
    public static function dayjs(): string
    {
        return self::scope("dayjs");
    }

    /**
     * request
     * @return string
     */
    public static function request(): string
    {
        return self::scope("request");
    }

    /**
     * hookRequest
     * @return string
     */
    public static function hookRequest(): string
    {
        return self::scope("hookRequest");
    }

    /**
     * 路由router对象
     * @return string
     */
    public static function router(): string
    {
        return self::this("\$router");
    }

    /**
     * 路由route对象
     * @return string
     */
    public static function route(): string
    {
        return self::this("\$route");
    }

    /**
     * window.$message对象
     * @return string
     */
    public static function message(): string
    {
        return self::window("\$message");
    }

    /**
     * 函数脚本
     * @param string $script
     * @return string
     */
    public static function func(string $script = ""): string
    {
        return scriptFunc($script);
    }

}
